package HomeWork1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class TestList {
    public static void main(String[] args) {
        String str =
                "张三丰,68,男\n" +
                        "张翠山,25,男\n" +
                        "殷素素,20,女\n" +
                        "何太冲,33,男\n" +
                        "班淑娴,34,女\n";
        // 1.将上述 数据封装为 List<Student> 集合，并使用迭代器遍历
        //创建集合
        List<Student> list=new ArrayList<>();
        //分割字符串
        String[] split = str.split("\n");
        //遍历字符串数组赋值
        for (String s : split) {
            //分割字符串s创建Student对象
            Student stu = getStudent(s);
            list.add(stu);
        }
        //使用迭代器遍历list集合
        Iterator<Student> it = list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

//        2.从控制台录入一个学生字符串，在集合中查找该字符串对应的学生对象是否存在
//        （姓名，年龄，性别都相同才能判断存在），录入格式为：张三丰,68,男
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学生对象:");
        String stu = sc.next();
        //分割字符串s创建Student对象
        Student student = getStudent(stu);
        boolean exists = exists(list, student);
        System.out.println(exists?"存在":"不存在");

        //3.接上题，找到集合中年纪最小的学生，并打印
        //遍历集合
        //定义最小年龄
        //方法一
        int minAge=list.get(0).getAge();
        for (Student student1 : list) {
            int age = student1.getAge();
            if(age<minAge){
                minAge=age;
            }
        }
       /* list.stream().filter(s->{
            int age = s.getAge();
            age==minAge;}).forEach(s-> System.out.println(s));*/   //Lambda式中只能使用final常量
        System.out.println("最小年龄为:");
        for (Student student1 : list) {
            if(student1.getAge()==minAge){
                System.out.println(student1);
            }
        }
        System.out.println("=========");
        //方法二
        //需考虑最小年龄为一个集合
        System.out.println("最小年龄为:");
        //创建新集合
        ArrayList<Student> ageList=new ArrayList<>();
        for (Student student1 : list) {
            if(student1.getAge()==minAge){
                ageList.add(student1);
            }
        }
        System.out.println(ageList);


        //4.接上题，删除集合中岁数大于 30 的对象
        System.out.println("删除后的集合为:");
        boolean b = list.removeIf(s -> s.getAge() > 30);
        System.out.println("删除是否成功:"+b);
        System.out.println(list);


    }

    private static boolean exists(List<Student> list, Student student) {
        //遍历集合查找对象是否存在
        for (Student student1 : list) {
            if(student.equals(student1)){
                return true;
            }
        }
        return false;
    }

    private static Student getStudent(String s) {
        //分割字符串对象
        String[] split1 = s.split(",");
        String name=split1[0];
        int age = Integer.parseInt(split1[1]);
        String sex=split1[2];
        return new Student(name,age,sex);
    }
}
